package cn.example.vip.mall.order.controller;

import cn.example.vip.mall.order.entity.Order;
import cn.example.vip.mall.order.service.OrderService;
import cn.example.vip.mall.util.RespCode;
import cn.example.vip.mall.util.RespResult;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private OrderService orderService;
	@Autowired
	private HttpServletRequest request;
	
	/***
	 * 添加订单
	 */
	@PostMapping
	public RespResult addOrder(@RequestBody Order order){
		System.err.println("addOrder:------->" + order);
		// 从过滤过的请求头获取用户名
		String userName = request.getHeader("username");	//"test";
		order.setUsername(userName);				// 用户名
		order.setId(IdWorker.getIdStr());			// 订单id
		order.setCreateTime(new Date());			// 创建时间
		order.setUpdateTime(order.getCreateTime());	// 修改时间
		order.setOrderStatus(0);					// 订单状态
		order.setPayStatus(0);						// 支付状态
		order.setIsDelete(0);						// 订单删除?
		
		//添加订单
		Boolean flag = orderService.addOrder(order);
		return flag ? RespResult.ok() : RespResult.error(RespCode.SYSTEM_ERROR);
	}
}
